/**********************************************************************
 * Copyright (C) 2006 ShenZhen SKY TECH.CO.,LTD.
 *
 * Class name : com.zte.frame.util.StringUtil
 *
 * Description :
 *
 * Create tag : HibernateCat 2006-2-23
 *
 * Modify tag : HibernateCat 2006-2-23
 *
 * Modified Description :
 *********************************************************************/
package com.aolong.archives.common.util;

import java.text.DecimalFormat;
import java.util.regex.*;

/**
 * 本公用类只在 JDK5.0 版本测试通过
 *
 * 对字符串进行常用操作的通用类，比如截取字符串、替代字符等。
 */
public class StringUtil {

	private StringUtil()
	{}
	
    public static final String PASSWOARD = "[[a-z]|[A-Z]|[0-9]]*";

    public static final String USERNAME = "[[a-z]|[A-Z]|[0-9]]*";

    public static final String NUMBER = "[0-9]*";

    public static final String LETTER = "[[A-Z]|[a-z]]*";

    public static final String PHONE_NO = "0[0-9]{2,3}-[0-9]{7,8}";

    public static final String MOBILE_PHONE_NO = "13[0-9][0-9]{8}";

    /**
     * 通过指定字符来截取字符串，并且返回截取后的字符串数组。例如：
     *
     * String[] s = StringUtil.split("HibernateCat","C");
     *
     * 那么s的值为{Hibernate,at}.
     *
     * @param str
     * @param pattern
     * @return String[]
     */

    public static String[] split(String str, String pattern) {
        Pattern p = Pattern.compile(pattern);
        return p.split(str);
    }

    /**
     * 将字符串分割成长整型数组
     *
     * @param str
     * @param pattern
     * @return
     */
    public static Long[] splitToLongArray(String str,String pattern){
    	String[] strings = split(str,pattern);
    	Long[] longs = new Long[strings.length];
    	for(int i=0;i<strings.length;i++){
    		longs[i] = Long.parseLong(strings[i]);
    	}
    	return longs;
    }

    /**
     * 将字符串分割成整型数组
     *
     * @param str
     * @param pattern
     * @return
     */
    public static Integer[] splitToIntArray(String str,String pattern){
    	String[] strings = split(str,pattern);
    	Integer[] ints = new Integer[strings.length];
    	for(int i=0;i<strings.length;i++){
    		ints[i] = Integer.parseInt(strings[i]);
    	}
    	return ints;
    }

    /**
     * 通过指定字符来截取字符串，并且返回截取的第position断字符。例如：
     *
     * String s = StringUtil.split("HibernateCat","C"，0);
     *
     * 那么s的值为"Hibernate".
     *
     * @param str
     * @param pattern
     * @param position
     * @return String[]
     */
    public static String split(String str, String pattern, int position) {
    	if(str != null && !str.equals("")){
	        Pattern p = Pattern.compile(pattern);
	        return p.split(str)[position];
    	}else{
    		return str;
    	}
    }

    /**
     * 使用str2代替str1中的pattern字符串（pattern可以是正则表达式）
     *
     * @param str1
     * @param pattern
     * @param str2
     * @return String
     */

    public static String replace(String str1, String pattern, String str2) {
        Pattern p = Pattern.compile(pattern);
       if(!StringUtil.isNullOrBlank(str1)){
    	   Matcher m = p.matcher(str1);
           return m.replaceAll(str2);
       }else{
    	   return null;
       }
       
    }

    /**
     * 判断str字符是否为我们定义的标准格式，format表示格式代码。
     *
     * @param str
     * @param format
     * @return
     */
    public static boolean isWellFormat(String str, String format) {
        Pattern p = Pattern.compile(format);
        Matcher m = p.matcher(str);
        return m.matches();
    }

    /**
     * 使用value数组中的值去依次替代resource中的pattern字符串(pattern可以是正则表达式)
     *
     * @param resource
     * @param pattern
     * @param value
     * @return
     */
    public static String replace(String resource, String pattern, String[] value) {
        Pattern p = Pattern.compile(pattern);
        Matcher m = null;
        for (int i = 0; i < value.length; i++) {
            m = p.matcher(resource);
            resource = m.replaceFirst(value[i]);
        }
        return resource;
    }

    /**
     * 使用replacement参数代替resource字符串中第一次出现符合pattern样式的字符串
     *
     * @param resource
     * @param pattern
     * @param replacement
     * @return
     */
    public static String replaceFirst(String resource, String pattern,
            String replacement) {
        Pattern p = Pattern.compile(pattern);
        Matcher m = p.matcher(resource);
        return m.replaceFirst(replacement);
    }

    /**
     * 使用replaceStr字符串去替代resource中的所有的pattern字符串(pattern可以是正则表达式)
     *
     * @param resource
     * @param replaceStr
     * @param pattern
     * @return
     */
    public static String replaceAll(String resource, String replaceStr,
            String pattern) {
        Pattern p = Pattern.compile(pattern);
        if(!StringUtil.isNullOrBlank(resource)){
        	Matcher m = p.matcher(resource);
            return m.replaceAll(replaceStr);
        }else{
        	return null;
        }
        
    }

    public static boolean isNullOrBlank(String str) {
        if (str == null) {
            return true;
        } else if (str.equals("")) {
            return true;
        } else {
            return false;
        }
    }

    public static String arrayToString(Object[] array) {
        if (array != null) {
            String returnString = "";
            for (Object str : array) {
            	if(str instanceof String){
            		returnString = returnString + ",'" + str.toString() + "'";
            	}else{
            		returnString = returnString + "," + str.toString();
            	}

            }
            return returnString.substring(1);
        } else {
            throw new RuntimeException(
                    "[Nullpoint Error]: the input parameter \"array\" is null.");
        }
    }

    public static String subString(String array,int start,int end){
    	int length = array.length();
    	if(start > length || end > length){
    		throw new RuntimeException(
    				"[NumberTooLarge] : String index out of range");
    	}
    	return array.substring(start,end);
    }

    /**
     * isParent 检测是否字符串是否匹配另一字符串的同级或者上级模式
     * @param array
     * @param compareArray 为需要匹配的字符串
     * @return
     */
    public static Boolean isParent(String array,String compareArray){

    	String[] arrays = StringUtil.split(array,"[-]");  //zte以前用“ _”
        String pattern = "";
        
        for(int i=0;i<(arrays.length-1);i++){
        	if(i==0){
        		pattern += arrays[0];
        	}else{
        		pattern += "|"+pattern+"-([0-9]+)"+"";
        	}
        }
        
        Boolean flag = compareArray.matches(pattern);

        return flag;
    }

    /**
     * 强制不使用科学计数法
     * @param o
     * @return
     */
    public static String getStrByTechMethod(Object o){  //强制不使用科学计数法
 	   String temp = null;
 	   DecimalFormat format = new DecimalFormat("###,###.00");
 	   if(o instanceof Double){
 		   temp = format.format(((Double)o).doubleValue());
 	   }
 	   if(o instanceof Long){
 		   temp = format.format(((Long)o).longValue());
 	   }else{
 		   temp = format.format(o);
 	   }
 	   return temp;
    }
    
	// 把Doulbe 格式化成0.00格式
	public static String getDouble(Double num) {
		DecimalFormat format = new DecimalFormat("0.00 ");
		return (format.format(num)).trim();
	}
	
	/**
	 * 
	 * 判断字符串是否是数字
	 * 如果input为""则返回true
	 * @param input
	 * @return
	 */
	public static boolean isNumber(String input) {
		if(input == null) return false;
		Pattern p = Pattern.compile("\\d*");
		Matcher m = p.matcher(input);
		return m.matches();
	}
	
    public static void main(String args[]) {
        String[] s = StringUtil.split("YYYY.MM.DD hh-mm-ss",
                "[.]|[-]|[#]|[~]|[ ]|[*]|[_]|[/]");
        for (String c : s) {
            System.out.println(c);
        }

        System.out.println(StringUtil.replace("YYYY.MM.DD hh-mm-ss",
                "[Y]{4}|[M]{2}|[D]{2}|[h]{2}|[m]{2}|[s]{2}", new String[] {
                        "1999", "10", "10", "12", "23", "23" }));
        System.out.println(StringUtil.replace("YYYY.MM.DD hh-mm-ss",
                "[Y]{4}|[M]{2}|[D]{2}|[h]{2}|[m]{2}|[s]{2}", new String[] {
                        "1999", "10", "10", "12", "23", "23" }));
        String[] account = { "1231", "21321", "1312321", "131232" };
        System.out.println(StringUtil.arrayToString(account));

        System.out.println(StringUtil.replace("2,333","[,]",""));
        String a = "1_6_1_1";
        System.out.println(StringUtil.replace("2006-12-12 12:29:90","[:]|[-]|[ ]",""));

        System.out.println(StringUtil.arrayToString(new Integer[]{1,2,3,4}));
        System.out.println(StringUtil.arrayToString(new String[]{"1","2","3","4"}));

        System.out.println(isParent(a,"1_6_2"));
    }

    /**
     * 左补齐字符串
     * 
     * @param strSource
     *            原字符串
     * @param nTotalSize
     *            补齐后的总长度
     * @param ch
     *            要补齐的字符
     * @return
     */
    public static String padLeft(String strSource, int nTotalSize, char ch)
    {
        int nCount = (strSource != null ? nTotalSize - strSource.length() : nTotalSize);

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < nCount; i++)
        {
            sb.append(ch);
        }
        if (strSource != null) sb.append(strSource);

        return sb.toString();
    }

    /**
     * 右补齐字符串
     * 
     * @param strSource
     *            原字符串
     * @param nTotalSize
     *            补齐后的总长度
     * @param ch
     *            要补齐的字符
     * @return
     */
    public static String padRight(String strSource, int nTotalSize, char ch)
    {
        int nCount = (strSource != null ? nTotalSize - strSource.length() : nTotalSize);

        StringBuilder sb = new StringBuilder();
        if (strSource != null) sb.append(strSource);
        for (int i = 0; i < nCount; i++)
        {
            sb.append(ch);
        }

        return sb.toString();
    }

}
